package com.upf1sh.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.Map;

@RestController
@RequestMapping("/api/external")
public class ExternalApiController {

    private final WebClient webClient;

    public ExternalApiController() {
        this.webClient = WebClient.builder()
                .baseUrl("https://ai-assistant-test.wodecorp.cn")
                .build();
    }

    @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatWithExternalApi(@RequestBody Map<String, Object> request) {
        return webClient.post()
                .uri("/chat/knowledge_base_chat")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(request)
                .retrieve()
                .bodyToFlux(String.class);
    }
}